#include "imgui/gut/App.h"

#define IMGUI_DEFINE_MATH_OPERATORS
#include "imgui/imnodes_internal.h"
#include "imgui/imnodes.h"

using namespace ImGui::Gut;

ImGui::Gut::App::App(const Ogl::Gut::App::Desc &desc)
    : Ogl::Gut::App(desc)
{

}

bool ImGui::Gut::App::Init()
{
    if (!Ogl::Gut::App::Init())
    {
        wtclog::warn("cannot create ogl gut App");
        return false;
    }

    if (!InitImGui())
    {
        wtclog::warn("cannot create Init Imgui");
        return false;
    }

    //ImGui::GetIO().AddKeyEvent();
    //io.KeyMap[]

    return true;
}

void ImGui::Gut::App::OnEdit()
{
}

void ImGui::Gut::App::Execute()
{
    while (!glfwWindowShouldClose(window))
    {
        glfwPollEvents();


        //if (m_Desc.m_Focused)
        {

            // Start the Dear ImGui frame
            ImGui_ImplOpenGL3_NewFrame();
            ImGui_ImplGlfw_NewFrame();
            ImGui::NewFrame();
            OnEdit();

            // Rendering
            ImGui::Render();
            OnFrameRender();
            OnTick();

            OnAfterUpdate();

            ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

            ImGuiIO& io = ImGui::GetIO();
            if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
            {
                GLFWwindow* backup_current_context = glfwGetCurrentContext();
                ImGui::UpdatePlatformWindows();
                ImGui::RenderPlatformWindowsDefault();
                glfwMakeContextCurrent(backup_current_context);
            }
        }

        glfwSwapBuffers(window);
    }
}

bool ImGui::Gut::App::InitImGui()
{

    // Setup Dear ImGui context
    IMGUI_CHECKVERSION();
    ImGui::CreateContext();
    ImGuiIO &io = ImGui::GetIO();
    (void)io;
    io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
    io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;  // Enable Gamepad Controls
    io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;     // Enable Docking
    // io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;   // Enable Multi-Viewport / Platform Windows
    // io.ConfigViewportsNoAutoMerge = true;
    // io.ConfigViewportsNoTaskBarIcon = true;

    // Setup Dear ImGui style
    // ImGui::StyleColorsDark();
    // ImGui::StyleColorsLight();
    ImGui::StyleColorsClassic();

    // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
    ImGuiStyle &style = ImGui::GetStyle();
    if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
    {
        style.WindowRounding = 0.0f;
        style.Colors[ImGuiCol_WindowBg].w = 1.0f;
    }

    // Setup Platform/Renderer backends
    ImGui_ImplGlfw_InitForOpenGL(window, true);
    ImGui_ImplOpenGL3_Init("#version 130");

    ImNodes::CreateContext();

    return true;
}

void ImGui::Gut::App::OnClose()
{
    Ogl::Gut::App::OnClose();

}

void ImGui::Gut::App::OnTick()
{
    Ogl::Gut::App::OnTick();
}

void ImGui::Gut::App::OnFrameBufferSize(int width, int height)
{
    Ogl::Gut::App::OnFrameBufferSize(width, height);
}


void ImGui::Gut::App::OnDestroy()
{

    ImNodes::DestroyContext();
    // Cleanup
    ImGui_ImplOpenGL3_Shutdown();
    ImGui_ImplGlfw_Shutdown();
    ImGui::DestroyContext();

    Ogl::Gut::App::OnDestroy();
}
